
import { createRouter, createWebHistory } from 'vue-router'
import { useAuthStore } from '../store'
import Home from '../views/Home.vue'
import Login from '../views/Login.vue'
import Dashboard from '../views/Dashboard.vue'
import File from '../views/file/file.vue'
import Management from '../views/management/management.vue'


const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    redirect: '/home/management',
    children: [
      {
        path: 'management',
        name: 'Management',
        component: Management
      },
      {
        path: 'file',
        name: 'File',
        component: File
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫，用于权限控制
router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()
  
  // 如果前往登录页面，直接允许访问
  if (to.path === '/login') {
    next()
    return
  }
  
  // 检查用户是否已登录，若未登录则跳转到登录页
  /*if (!authStore.isLoggedIn) {
    next('/login')
    return
  }*/
  
  // 用户已登录，允许访问
  next()
})

export default router